package cn.iocoder.yudao.module.testequipment.controller.admin.testequipment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.testequipment.controller.admin.testequipment.vo.*;
import cn.iocoder.yudao.module.testequipment.dal.dataobject.testequipment.TestEquipmentDO;
import cn.iocoder.yudao.module.testequipment.service.testequipment.TestEquipmentService;

@Tag(name = "管理后台 - 参试设备管理表")
@RestController
@RequestMapping("/testequipment/test-equipment")
@Validated
public class TestEquipmentController {

    @Resource
    private TestEquipmentService testEquipmentService;

    @PostMapping("/create")
    @Operation(summary = "创建参试设备管理表")
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:create')")
    public CommonResult<Long> createTestEquipment(@Valid @RequestBody TestEquipmentSaveReqVO createReqVO) {
        return success(testEquipmentService.createTestEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参试设备管理表")
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:update')")
    public CommonResult<Boolean> updateTestEquipment(@Valid @RequestBody TestEquipmentSaveReqVO updateReqVO) {
        testEquipmentService.updateTestEquipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参试设备管理表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:delete')")
    public CommonResult<Boolean> deleteTestEquipment(@RequestParam("id") Long id) {
        testEquipmentService.deleteTestEquipment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除参试设备管理表")
                @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:delete')")
    public CommonResult<Boolean> deleteTestEquipmentList(@RequestParam("ids") List<Long> ids) {
        testEquipmentService.deleteTestEquipmentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参试设备管理表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:query')")
    public CommonResult<TestEquipmentRespVO> getTestEquipment(@RequestParam("id") Long id) {
        TestEquipmentDO testEquipment = testEquipmentService.getTestEquipment(id);
        return success(BeanUtils.toBean(testEquipment, TestEquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参试设备管理表分页")
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:query')")
    public CommonResult<PageResult<TestEquipmentRespVO>> getTestEquipmentPage(@Valid TestEquipmentPageReqVO pageReqVO) {
        PageResult<TestEquipmentDO> pageResult = testEquipmentService.getTestEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TestEquipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参试设备管理表 Excel")
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTestEquipmentExcel(@Valid TestEquipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TestEquipmentDO> list = testEquipmentService.getTestEquipmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参试设备管理表.xls", "数据", TestEquipmentRespVO.class,
                        BeanUtils.toBean(list, TestEquipmentRespVO.class));
    }
    
    @GetMapping("/my-use-page")
    @Operation(summary = "获得我的设备使用记录分页（已出库）")
    @PreAuthorize("@ss.hasPermission('testequipment:test-equipment:query')")
    public CommonResult<PageResult<TestEquipmentMyUseRespVO>> getMyEquipmentUsePage(@Valid TestEquipmentPageReqVO pageReqVO) {
        PageResult<TestEquipmentMyUseRespVO> pageResult = testEquipmentService.getMyEquipmentUsePage(pageReqVO);
        return success(pageResult);
    }
    // TestEquipmentController.java 新增接口
@PutMapping("/update-status")
@Operation(summary = "更新设备使用状态")
@PreAuthorize("@ss.hasPermission('testequipment:test-equipment:update')")
public CommonResult<Boolean> updateTestEquipmentStatus(@Valid @RequestBody TestEquipmentStatusReqVO reqVO) {
    testEquipmentService.updateTestEquipmentStatus(reqVO.getId(), reqVO.getUseStatus());
    return success(true);
}

}